<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AJAX POST 请求</title>
    <style>
        #result{
            width:200px;
            height:100px;
            border:solid 1px #903;
        }
    </style>
</head>
<body>
    <div id="result"></div>
    <script>
         var result = document.getElementById('result')
         result.onmouseover = function () {
             //1. 创建对象
             var xhr = new XMLHttpRequest()
             //2.初始化
             xhr.open('POST','http://127.0.0.1:8000/serverPost')

             // 创建一个URLSearchParams对象吧data数据转换成 参数=值&参数=值格式
             const data = {
                 name: 'zzl',
                 status: '帅'
             }
             const usp = new URLSearchParams(data)
             const query = usp.toString()


             // 设置对应的content-type
             xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
             //3.发送
             xhr.send(query)
             //4.监听状态改变事件
             xhr.onreadystatechange = function () {
                 //服务器返回全部信息的时候
                 if (xhr.readyState === 4){
                     //改变页面内容
                     result.innerHTML = xhr.response
                 }
             }
         }
    </script>
</body>
</html>